package org.hbnu.xiaomi.controller;

import org.hbnu.xiaomi.entity.Goods;
import org.hbnu.xiaomi.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

//请求控制器
@Controller
public class GoodsController {
    //注明需要商品服务对象
    @Autowired
    GoodsService goodsService;
    //按类别事实获取商品
    @RequestMapping(value = "/getGoodsByTypesId")
    @ResponseBody
    public List<Goods> getGoodsByTypesId(@RequestBody Map<String,String> maps){
        System.out.println("开始获取商品");

        //获取商品的分类编号
        int types_id=Integer.parseInt(maps.get("types_id"));
        System.out.println("商品分类编号是："+types_id);

        //获取商品
        List<Goods> goodsList = goodsService.getByTypesId(types_id);
        System.out.println("获取商品个数："+goodsList.size());

        return goodsList;


    }
    //查看商品详情
    @RequestMapping(value = "/getById")
    @ResponseBody
    public Goods getById(@RequestBody Map<String,String> maps){

        System.out.println("开始查看商品详情");


        int id=Integer.parseInt(maps.get("id"));
        System.out.println("商品编号是："+id);

        Goods goods = goodsService.getById(id);
        if(goods==null){
            System.out.println("没有此商品");
            return null;
        }
        System.out.println("商品的信息是"+goods.getName());
        return goods;
    }

}
